﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using DotConferenceSql;
using NUnit.Framework.SyntaxHelpers;
using System.Data.SqlClient;

namespace TestDotConferenceSql
{
    [TestFixture]
    public class TestCompositeQueryElement
    {
        CompositeQueryElement compositeElement;

        [SetUp]
        public void Init()
        {
            compositeElement = new CompositeQueryElement();
        }

        [Test]
        public void ElementSort()
        {
            SelectQueryElement selectElement = new SelectQueryElement();
            selectElement.Add("field1");

            WhereClauseQueryElement whereElement = new WhereClauseQueryElement();
            whereElement.Elements.Add(new CustomWhereStatement("field1 = 1"));

            OrderByQueryElement orderByElement = new OrderByQueryElement("field1", OrderByQueryElement.Order.Ascendant);

            GroupByQueryElement groupByElement = new GroupByQueryElement();
            groupByElement.Add("field1");

            compositeElement.Elements.Add(groupByElement);
            compositeElement.Elements.Add(selectElement);
            compositeElement.Elements.Add(orderByElement);
            compositeElement.Elements.Add(whereElement);
            compositeElement.Elements.Add(new FromTableQueryElement("Test"));

            Assert.That(compositeElement.Elements[0], Is.TypeOf(typeof(GroupByQueryElement)));
            Assert.That(compositeElement.Elements[1], Is.TypeOf(typeof(SelectQueryElement)));
            Assert.That(compositeElement.Elements[2], Is.TypeOf(typeof(OrderByQueryElement)));
            Assert.That(compositeElement.Elements[3], Is.TypeOf(typeof(WhereClauseQueryElement)));
            Assert.That(compositeElement.Elements[4], Is.TypeOf(typeof(FromTableQueryElement)));

            // Sort Element
            string textQuery = compositeElement.BuildTextQuery();

            Assert.That(compositeElement.Elements[0], Is.TypeOf(typeof(SelectQueryElement)));
            Assert.That(compositeElement.Elements[1], Is.TypeOf(typeof(FromTableQueryElement)));
            Assert.That(compositeElement.Elements[2], Is.TypeOf(typeof(WhereClauseQueryElement)));
            Assert.That(compositeElement.Elements[3], Is.TypeOf(typeof(GroupByQueryElement)));
            Assert.That(compositeElement.Elements[4], Is.TypeOf(typeof(OrderByQueryElement)));
        }

        [Test]
        public void NoElements()
        {
            Assert.That(compositeElement.Elements.Count, Is.EqualTo(0));
            Assert.That(compositeElement.BuildTextQuery(), Is.Empty);
        }

        [Test]
        public void ListParameters()
        {
            UpdateQueryElement updateElement = new UpdateQueryElement();
            updateElement.Table = "Test";
            updateElement.Add("field1", 1);

            WhereClauseQueryElement whereElement = new WhereClauseQueryElement();
            whereElement.Elements.Add(new IdWhereStatement(10));

            compositeElement.Elements.Add(updateElement);
            compositeElement.Elements.Add(whereElement);

            List<SqlParameter> parameters = compositeElement.ListParameters();

            Assert.That(parameters.Count, Is.EqualTo(2));
            Assert.That(parameters[0].Value, Is.EqualTo(1));
            Assert.That(parameters[1].Value, Is.EqualTo(10));
        }
    }
}
